package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qinglei.recoup.common.annotation.NotBlank;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.system.dao.UserWechatCommentMapper;
import com.qinglei.recoup.system.domain.UserWechat;
import com.qinglei.recoup.system.dao.UserWechatMapper;
import com.qinglei.recoup.system.domain.UserWechatComment;
import com.qinglei.recoup.system.service.UserWechatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author dqh
 */
@Service
public class UserWechatServiceImpl extends ServiceImpl<UserWechatMapper, UserWechat> implements UserWechatService {

    @Autowired
    private UserWechatMapper  userWechatMapper;

    @Autowired
    private UserWechatCommentMapper userWechatCommentMapper;

    @Override
    public CommonRes add(UserWechat userWechat) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("open_id",userWechat.getOpenId());
        queryWrapper.eq("is_deleted",0);
        UserWechat userWechatTmp = userWechatMapper.selectOne(queryWrapper);
        if(userWechatTmp!=null){
         return CommonRes.failed().setMsg("该用户已绑定！");
        }
        userWechatMapper.insert(userWechat);
        return  CommonRes.ok().setMsg("绑定成功！");
    }

    @Override
    public UserWechat getByOpenId(String openId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("open_id",openId);
        wrapper.eq("is_deleted",0);
        return userWechatMapper.selectOne(wrapper);

    }

    @Override
    public CommonRes comment(UserWechatComment userWechatComment) {

        QueryWrapper<UserWechatComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bed_id",userWechatComment.getBedId());
        queryWrapper.eq("open_id",userWechatComment.getOpenId());
        queryWrapper.eq("statistics_date",userWechatComment.getStatisticsDate());
        if(userWechatCommentMapper.selectOne(queryWrapper)!=null){
            return CommonRes.failed().setMsg("已评价！");
        }
        userWechatCommentMapper.insert(userWechatComment);
        return  CommonRes.ok().setMsg("评价成功！");
    }

    @Override
    public CommonRes isComment(UserWechatComment userWechatComment) {
        QueryWrapper<UserWechatComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bed_id",userWechatComment.getBedId());
        queryWrapper.eq("open_id",userWechatComment.getOpenId());
        queryWrapper.eq("statistics_date",userWechatComment.getStatisticsDate());
        if(userWechatCommentMapper.selectOne(queryWrapper)!=null){
            return CommonRes.failed().setMsg("已评价！");
        }
        return  CommonRes.ok().setMsg("未评价！");
    }


}
